package com.oa.android.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.webkit.WebView;
import android.widget.Toast;

import com.oa.android.MainActivity;
import com.oa.android.base.BaseActivity;
import com.oa.android.R;
import com.oa.android.listener.PermissionListener;
import com.oa.android.utils.UConstants;
import com.oa.android.utils.Util;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import org.xutils.x;

import java.util.List;

/**
 * 启动页
 * Created by zouqh on 2016/5/23.
 */
public class WelcomeActivity extends BaseActivity {
    private Util util;
    private WebView webView;

    // C0:18:5A:E7:87:28:94:5D:33:87:29:0B:3E:D4:BD:71:09:B3:8C:5B
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.Ext.init(getApplication());
        // 设置是否输出debug
        x.Ext.setDebug(true);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.weicome_activity);
        webView = (WebView) findViewById(R.id.webview);
        util = new Util(this);
//        webView.loadUrl("http://123.57.236.11:8082/Samples/ycsq.html");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermission();
        }else{
            setMain();
        }



    }


    private void setMain(){
        final String name = util.getUser("name");
        final String password = util.getUser("password");
        String mindpass = util.getUser("mindpass");
        String strIp = util.getUser("ip");
        String strPost = util.getUser("post");
        Util.printLog("strIp-----------------", strIp);
        Util.printLog("strPost-----------------", strPost);
        if (Util.NoNull(strIp) && Util.NoNull(strPost)) {
            UConstants.WEB_URL = "http://" + strIp + ":" + strPost + "/";
            UConstants.URL_DOWN = "http://" + strIp + ":" + strPost + "";
            UConstants.DZ = UConstants.WEB_URL;
            new UConstants().setIp();
        }
        if (Util.NoNull(mindpass) && mindpass.equals("1")) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Login(name, password);
                }
            }, 2000);
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                    finish();
                }
            }, 2000);
        }
    }
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:

                    break;
                case 0:

                    break;
            }
        }
    };


    //用户登录
    private void Login(final String name, final String pass) {
        Util.printLog("Login------------UConstants.LOGIN_URL-----", UConstants.LOGIN_URL);
        RequestParams params = new RequestParams(UConstants.LOGIN_URL);
        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("EmpId", name);
        params.addBodyParameter("Pwd", pass);
//        Map<String,String> map=new HashMap<>();
//        map.put("LKind", "CBJ");
//        map.put("EmpId", name);
//        map.put("Pwd", pass);
//        HttpHandler httpHandler=new HttpHandler();
//       String result= httpHandler.HttpPost(map,UConstants.LOGIN_URL);
//        Util.printLog("Login------------onSuccess-----", result);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getMerchants------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);
                        boolean success = object.getBoolean("success");
                        String msg = null;
                        if (success) {

                            UConstants.EmpId = object.getString("EmpId");
                            UConstants.EmpName = object.getString("EmpName");
                            UConstants.USERBUMEN = object.getString("DepName");
                            UConstants.Powers = object.getString("RoleName");
//                            UConstants.TRUENAME= object.getString("TRUENAME");
//                            UConstants.ShowTBSL= object.getString("ShowTBSL");
                            UConstants.CB_ROOT = object.getString("IP12");
                            UConstants.XX_ROOT = object.getString("IP04");
                            com.watermeter.android.utils.UConstants.EmpId = object.getString("EmpId");
                            com.watermeter.android.utils.UConstants.EmpName = object.getString("EmpName");
                            com.watermeter.android.utils.UConstants.Powers = object.getString("RoleName");
                            com.watermeter.android.utils.UConstants.DepName = object.getString("DepName");
                            com.watermeter.android.utils.UConstants.WEB_URL = "http://" + object.getString("IP12") + ":" + object.getString("PORT12") + "/BLL/";
                            com.android.xunxian.utils.UConstants.EmpId = object.getString("EmpId");
                            com.android.xunxian.utils.UConstants.EmpName = object.getString("EmpName");
                            com.android.xunxian.utils.UConstants.RoleId = object.getString("RoleId");
                            com.android.xunxian.utils.UConstants.RoleName = object.getString("RoleName");
                            com.android.xunxian.utils.UConstants.Powers = object.getString("Powers");
                            com.android.xunxian.utils.UConstants.WEB_URL = "http://" + object.getString("IP04") + ":" + object.getString("PORT04") + "/BLL/";
                            UConstants.DepId = object.getString("DepId");
                            if (object.toString().contains("ComId")) {
                                UConstants.ComId = object.getString("ComId");
                            }
                            UConstants.TokenId = object.getString("TokenId");
                            if (object.toString().contains("ComName")) {
                                UConstants.ComName = object.getString("ComName");
                            }
                            util.saveInfo("EmpId",object.getString("EmpId"));
                            if (object.toString().contains("ShowTBSL")) {
                                com.watermeter.android.utils.UConstants.ShowTBSL = object.getString("ShowTBSL");
                                com.android.xunxian.utils.UConstants.ShowTBSL = object.getString("ShowTBSL");
                            }
//                            UConstants.DepId = object.getString("DepId");
                            if (object.toString().contains("DepName")) {
                                com.watermeter.android.utils.UConstants.DepName = object.getString("DepName");
                                com.android.xunxian.utils.UConstants.DepName = object.getString("DepName");
                            }
                            if (object.toString().contains("ComName")) {
                                com.watermeter.android.utils.UConstants.ComName = object.getString("ComName");
                                com.android.xunxian.utils.UConstants.ComName = object.getString("ComName");
                            }
                            new com.watermeter.android.utils.UConstants().setIp();
                            new com.android.xunxian.utils.UConstants().setIp();
                            msg = "登录成功！";
                            startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
                            finish();
                        } else {
                            msg = object.getString("msg");
                            startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                            finish();
                        }

                        Toast.makeText(x.app(), msg, Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                        finish();
                        e.printStackTrace();
                    }
                }
            }


            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

//                Toast.makeText(x.app(), "获取数据失败！", Toast.LENGTH_LONG).show();
                startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                finish();
            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
            }

            @Override
            public void onFinished() {

            }
        });
    }

    //授权权限
    private void requestPermission() {
        Log.e("requestPermission--------","");
        requestRunTimePermission(new String[]{
                Manifest.permission.CALL_PHONE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.CAMERA,Manifest.permission.READ_PHONE_STATE,Manifest.permission.READ_CALL_LOG
                        ,Manifest.permission.WRITE_CALL_LOG,Manifest.permission.ADD_VOICEMAIL
                        ,Manifest.permission.PROCESS_OUTGOING_CALLS,Manifest.permission.READ_CALENDAR
                        ,Manifest.permission.WRITE_CALENDAR}
                , new PermissionListener() {
                    @Override
                    public void onGranted() {  //所有权限授权成功
                        Log.i("全部授权","AAAAAAAAAAAAAAAAAAAA");
                        setMain();
                    }

                    @Override
                    public void onGranted(List<String> grantedPermission) { //授权失败权限集合

                        System.out.println("onGranted---"+grantedPermission);
                    }

                    @Override
                    public void onDenied(List<String> deniedPermission) { //授权成功权限集合
                        System.out.println("onDenied----"+deniedPermission);
                        setMain();
                    }
                });
    }
}
